/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.util;

import java.util.Map;

public class MapUtils {
    /**
     * 安全获取字符串字段值
     */
    public static String getStringValue(Map<String, Object> dataMap, String key) {
        Object value = dataMap.get(key);
        if (value instanceof String) {
            return (String) value;
        } else if (value != null) {
            return value.toString(); // 非String类型尝试转换
        }
        return null;
    }

    /**
     * 安全获取Double字段值
     */
    public static Double getDoubleValue(Map<String, Object> dataMap, String key) {
        Object value = dataMap.get(key);
        if (value instanceof Number) {
            return ((Number) value).doubleValue();
        } else if (value instanceof String) {
            try {
                return Double.parseDouble((String) value);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }

    /**
     * 安全获取Integer字段值
     */
    public static Integer getIntegerValue(Map<String, Object> dataMap, String key) {
        Object value = dataMap.get(key);
        if (value instanceof Number) {
            return ((Number) value).intValue();
        } else if (value instanceof String) {
            try {
                return Integer.parseInt((String) value);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }

    /**
     * 安全获取Long字段值
     */
    public static Long getLongValue(Map<String, Object> dataMap, String key) {
        Object value = dataMap.get(key);
        if (value instanceof Number) {
            return ((Number) value).longValue();
        } else if (value instanceof String) {
            try {
                return Long.parseLong((String) value);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }

    /**
     * 安全获取Boolean字段值
     */
    public static Boolean getBooleanValue(Map<String, Object> dataMap, String key) {
        Object value = dataMap.get(key);
        if (value instanceof Boolean) {
            return (Boolean) value;
        } else if (value instanceof String) {
            String strValue = ((String) value).trim().toLowerCase();
            return "true".equals(strValue) || "yes".equals(strValue) || "1".equals(strValue);
        } else if (value instanceof Number) {
            return ((Number) value).intValue() != 0;
        }
        return null;
    }

    /**
     * 安全获取Float字段值
     */
    public static Float getFloatValue(Map<String, Object> dataMap, String key) {
        Object value = dataMap.get(key);
        if (value instanceof Number) {
            return ((Number) value).floatValue();
        } else if (value instanceof String) {
            try {
                return Float.parseFloat((String) value);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }

    /**
     * 安全获取Short字段值
     */
    public static Short getShortValue(Map<String, Object> dataMap, String key) {
        Object value = dataMap.get(key);
        if (value instanceof Number) {
            return ((Number) value).shortValue();
        } else if (value instanceof String) {
            try {
                return Short.parseShort((String) value);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }

    /**
     * 安全获取Boolean字段值（严格模式，只接受true/false字符串）
     */
    public static Boolean getStrictBooleanValue(Map<String, Object> dataMap, String key) {
        Object value = dataMap.get(key);
        if (value instanceof Boolean) {
            return (Boolean) value;
        } else if (value instanceof String) {
            String strValue = ((String) value).trim().toLowerCase();
            return "true".equals(strValue) || "false".equals(strValue);
        } else if (value instanceof Number) {
            return ((Number) value).intValue() != 0;
        }
        return null;
    }
}
